package com.power.powercash;

import com.power.visual.BlackLabel;
import com.power.visual.PaintStyle;
import java.awt.Component;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.event.RowSorterEvent;
import javax.swing.event.RowSorterListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author f6036477
 */
public class ColumnHeader implements TableCellRenderer, RowSorterListener, TableModelListener
{

  private BlackLabel cell;

  private boolean sort;

  private boolean changed;

  private boolean icon_on;

  private ColumnHeaderGroup group;


  public ColumnHeader()
  {
    this(PaintStyle.BLUE_STYLE);
  }

  public ColumnHeader(PaintStyle style)
  {
    if(style == null) style = PaintStyle.BLUE_STYLE;
    cell = new BlackLabel(style, "");
    cell.setHorizontalAlignment(SwingConstants.CENTER);
    cell.setImageGap(10);
    sort = false;
    changed = false;
    icon_on = false;
  }

  @Override
  public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
  {
    cell.setText(value.toString());

    if(icon_on) {
      cell.setHorizontalAlignment(SwingConstants.RIGHT);
      if(sort)
        cell.setImageIcon(ImageSet.SORT_UP_ARROW);
      else
        cell.setImageIcon(ImageSet.SORT_DOWN_ARROW);
    }//if
    else
      cell.setImageIcon(null);

    return cell;
  }


  public boolean isIconOn()
  {
    return icon_on;
  }

  public void setIconOn(boolean on)
  {
    this.icon_on = on;
  }

  public void sorterChanged(RowSorterEvent e)
  {
    RowSorterEvent.Type type = e.getType();
    if(type == RowSorterEvent.Type.SORTED)
      System.out.println("SORTED");
    else
      System.out.println("SORT_ORDER_CHANGED");
    if(!changed) {
      changed = !changed;
      return;
    }
    icon_on = true;
    sort = !sort;
    changed = !changed;
  }

  @Override
  public void tableChanged(TableModelEvent e)
  {
    System.out.println("e.getColumn(): "+ e.getColumn());
  }

}
